package com.syh.jn.scm.infrastructure.repository.impl.supplier.base;

import cn.redscarf.commons.util.other.ExtStringUtils;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.supplier.entity.base.Storehouse;
import com.syh.jn.scm.domain.supplier.repository.base.StorehouseRepository;
import com.syh.jn.scm.dto.supplier.base.storeHouse.StorehouseVo;
import com.syh.jn.scm.infrastructure.repository.model.base.StorehouseModel;
import com.syh.jn.scm.infrastructure.repository.model.stock.StockGoodsModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

/**
 * 仓库管理
 */
@Repository
public class StorehouseRepositoryImpl extends AbstractAggregateRepository<Storehouse, StorehouseModel, Long> implements StorehouseRepository {

  @Override
  public Long add(Storehouse storehouse) {
    Long id = super.add(storehouse);
    StorehouseModel storehouseModel = this.hibernateTemplate.get(StorehouseModel.class, id);
    storehouseModel.setCode(String.format("%06d", id));
    return id;
  }

  @Override
  public void update(Storehouse storehouse) {
    super.update(storehouse);
    if (ExtStringUtils.isEmpty(storehouse.getCode())) {
      StorehouseModel storehouseModel = this.hibernateTemplate.get(StorehouseModel.class, storehouse.getId());
      storehouseModel.setCode(String.format("%06d", storehouse.getId()));
    }
  }


  /**
   * 重写remove方法
   *
   * @param storehouse
   */
  @Override
  public void remove(Storehouse storehouse) {
    //删除仓库基本信息
    super.removeById(storehouse.getId());

    //删除当前仓库的所有商品
    //根据当前仓库id查找对应的库存商品
    StringBuffer hql = new StringBuffer();
    hql.append("from StockGoodsModel model where model.storehouseId = ?");
    List<StockGoodsModel> goodsModelList = (List<StockGoodsModel>) this.hibernateTemplate.find(hql.toString(), storehouse.getId());

    //移除所有符合条件的库存商品
    goodsModelList.stream().forEach(goodsModel -> {
      this.hibernateTemplate.delete(goodsModel);
    });
  }

  @Override
  protected SQL getViewSql() {
    return new SQL() {{
      SELECT(
              SQLAliasGenerateUtil.aliasGenerate(StorehouseModel.class, "storehouse", null)
      );
      FROM("T_BASE_STOREHOUSE storehouse");
    }};
  }

  @Override
  protected Class getViewObject() {
    return StorehouseVo.class;
  }

  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    HashMap<String, String> map = new HashMap();
    map.put("id", "storehouse.ID");
    map.put("name", "storehouse.NAME");
    map.put("status", "storehouse.STATUS");
    map.put("purpose", "storehouse.PURPOSE");
    map.put("type", "storehouse.TYPE");
    map.put("isPerchaseToSale", "storehouse.IS_PERCHASE_TO_SALE");
    map.put("businessEntityId", "storehouse.BUSINESS_ENTITY_ID");
    ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
      return map.get(propertyName);
    });
    return parse;
  }
}
